import requests
from typing import List, Dict
from dataclasses import dataclass
from app.db.database import SessionLocal
from app.db.crud import create_tpost
from app.pacong.content_fetcher import ContentFetcher

@dataclass
class DouyinHotItem:
    """抖音热搜条目的数据类"""
    sentence_id: str
    word: str
    event_time: str
    hot_value: str

class DouyinHotSearchCrawler(ContentFetcher):
    """
    用于从抖音网站获取热搜数据并处理的类。
    """

    def __init__(self):
        """
        初始化类实例。
        """
        self.base_url = "https://www.douyin.com"
        self.api_url = "https://www.douyin.com/aweme/v1/web/hot/search/list/"
        self.login_url = "https://www.douyin.com/passport/general/login_guiding_strategy/"
        self.session = requests.Session()
        self.content_type = 8  # 抖音的source_id设为8

    def get_cookies(self) -> Dict[str, str]:
        """
        获取抖音网站的cookies。

        :return: 包含cookies的字典
        :raises: 如果请求失败，抛出HTTPError
        """
        params = {
            'aid': '6383'
        }
        
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        
        response = self.session.get(self.login_url, params=params, headers=headers)
        response.raise_for_status()
        return self.session.cookies.get_dict()

    def fetch_data(self) -> Dict:
        """
        获取热搜数据。

        :return: API响应的JSON数据
        :raises: 如果请求失败，抛出HTTPError
        """
        # 首先获取cookies
        cookies = self.get_cookies()
        
        params = {
            'device_platform': 'webapp',
            'aid': '6383',
            'channel': 'channel_pc_web',
            'detail_list': '1'
        }
        
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        
        response = self.session.get(
            self.api_url,
            params=params,
            headers=headers
        )
        response.raise_for_status()
        return response.json()

    def fetch_content(self) -> List[Dict]:
        """
        获取并处理热搜数据。

        :return: 返回处理后的数据列表。
        """
        # 获取API响应
        response_data = self.fetch_data()
        
        # 验证响应数据结构
        if 'data' not in response_data or 'word_list' not in response_data['data']:
            raise ValueError("API返回数据结构异常")
            
        result = []
        for item in response_data['data']['word_list']:
            # 构建热搜页面URL
            hot_url = f"{self.base_url}/hot/{item['sentence_id']}"
            
            # 添加到结果列表
            result.append({
                "id": item['sentence_id'],
                "title": item['word'],
                "url": hot_url,
                "pub_date": item.get('event_time'),
                "extra": {
                    "event_time": item.get('event_time'),
                    "hot_value": item.get('hot_value')
                }
            })
        
        return result


# 示例调用
if __name__ == "__main__":
    # 创建类实例
    crawler = DouyinHotSearchCrawler()
    
    # 获取并保存数据
    crawler.fetch_and_save() 